package com.tao.plan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.plan.common.BaseThreadLocal;
import com.tao.plan.domain.pojo.PCommonPlan;
import com.tao.plan.domain.pojo.PCountDownDay;
import com.tao.plan.domain.vo.CommonPlanCountVo;
import com.tao.plan.mapper.PCommonPlanMapper;
import com.tao.plan.mapper.PCountDownDayMapper;
import com.tao.plan.service.PCommonPlanService;
import com.tao.plan.service.PCountDownDayService;
import com.tao.plan.service.PCountPlanService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PCountDownDayServiceImpl extends ServiceImpl<PCountDownDayMapper, PCountDownDay> implements PCountDownDayService {

	@Override
	public List<PCountDownDay> listByDateTime(Integer status) {
		LocalDate now = LocalDate.now();
		LambdaQueryWrapper<PCountDownDay> lqw = new LambdaQueryWrapper<PCountDownDay>()
				.eq(PCountDownDay::getUid, BaseThreadLocal.getUid())
				.orderByDesc(PCountDownDay::getTargetTime);
		// 按照targetTime进行分类, 分为倒计时中和已过期, 过期时间精确到日即可
		if (status == 0){
			// 未过期
			lqw.apply("DATE(target_time) >= {0}", now); // 比较 targetTime 的日期部分与当前日期
		}else {
			// 已经过期
			lqw.apply("DATE(target_time) < {0}", now); // 比较 targetTime 的日期部分与当前日期
		}
		return this.baseMapper.selectList(lqw);
	}

	@Override
	public List<PCountDownDay> listByDateTime(Integer status, Long userId) {
		LocalDate now = LocalDate.now();
		LambdaQueryWrapper<PCountDownDay> lqw = new LambdaQueryWrapper<PCountDownDay>()
				.eq(PCountDownDay::getUid, userId)
				.orderByDesc(PCountDownDay::getTargetTime);
		// 按照targetTime进行分类, 分为倒计时中和已过期, 过期时间精确到日即可
		if (status == 0){
			// 未过期
			lqw.apply("DATE(target_time) >= {0}", now); // 比较 targetTime 的日期部分与当前日期
		}else {
			// 已经过期
			lqw.apply("DATE(target_time) < {0}", now); // 比较 targetTime 的日期部分与当前日期
		}
		return this.baseMapper.selectList(lqw);
	}

	@Override
	public List<PCountDownDay> listByDay(String calendar) {
		LambdaQueryWrapper<PCountDownDay> lqw = new LambdaQueryWrapper<>();
		lqw
				.eq(PCountDownDay::getUid,BaseThreadLocal.getUid())
				.apply("DATE(target_time) = {0}", calendar);
		return this.baseMapper.selectList(lqw);
	}

}
